package com.zwps.common.mybatis.plus.interceptor;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 添加的数据过滤条件只在当前线程有效
 * 
 * @ClassName: ThreadLocalDataPermissionnterceptor
 * @Description:
 * @author lyh
 * @date 2023年9月21日 下午5:18:32
 *
 */
public class ThreadLocalDataPermissionInterceptor extends AbstractDataPermissionInterceptor {

    protected final ThreadLocal<Map<String, List<Function<Table, Expression>>>> THREAD_LOCAL_TARGET_TABLE = new ThreadLocal<Map<String, List<Function<Table, Expression>>>>();


    @Override
    public synchronized void addTableExpressionList(String tableName, List<Function<Table, Expression>> list) {
        Map<String, List<Function<Table, Expression>>> map = THREAD_LOCAL_TARGET_TABLE.get();
        if (map == null) {
            THREAD_LOCAL_TARGET_TABLE.set(map = new HashMap<String, List<Function<Table, Expression>>>());
        }
        map.put(tableName, list);
    }

    public void clear() {
        THREAD_LOCAL_TARGET_TABLE.remove();
    }
    
    @Override
    public synchronized Map<String, List<Function<Table, Expression>>> getTables() {
        Map<String, List<Function<Table, Expression>>> map = THREAD_LOCAL_TARGET_TABLE.get();
        if (map == null) {
            THREAD_LOCAL_TARGET_TABLE.set(map = new HashMap<String, List<Function<Table, Expression>>>());
        }
        return map;
    }
}
